package com.yy.yyui.popup
{
	import com.maqie.config.Config;
	import com.util.tools.ArrayTools;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 *鼠标热感 
	 * @author NB_XX
	 * 
	 */
	public class Tooltips
	{
		/**热感默认容器**/
		public static var defaultRoot:DisplayObjectContainer;
		public static const TOOLTIPS_UPDATE:String = "tooltipsupdate";
		/**默认监听时间**/
		private static const DEFAULT_CONDITION:Array = [MouseEvent.MOUSE_OVER,MouseEvent.MOUSE_OUT,Tooltips.TOOLTIPS_UPDATE];
		
		private static var tipsObject:Array = new Array();
		private static var showingTips:DisplayObject;
		
		
		//targetName
		//modifier
		//context
		//tips
		//showconditions
		//container
		private static function findTipsConfig(target:DisplayObject):Object{
			for(var i:uint = 0,l:uint = tipsObject.length;i<l;i++){
				if(tipsObject[i].target==target){
					return tipsObject[i];
				}
			}
			return null;
		}
		
		
		public static function addTips(target:DisplayObject,context:Object=null,conditions:Array=null,modifyTips:Function=null,tipsContainer:DisplayObjectContainer = null,x:int = 0,y:int = 0):void{
			if(defaultRoot==null){
				if(tipsContainer == null){
					defaultRoot = target.parent;
				}else{
					defaultRoot = tipsContainer;
				}
			}
			
			if(modifyTips == null){
				modifyTips = defaultModifier;
			}
			if(context == null){
				context = target;
			}
			
			if(conditions == null||conditions.length==0){
				conditions = DEFAULT_CONDITION;
			}
			
			if(tipsContainer == null){
				tipsContainer = defaultRoot;
			}
			
			var obj:Object = findTipsConfig(target);
			
			var i:uint,l:uint;
			if(obj == null){
				obj = new Object();
				tipsObject.push(obj);
			}else{
				if(obj.tips != null)
				{
					obj.tips.visible = false;	
				}
				for(i=0,l =obj.conditions.length;i<l;i++){
					target.removeEventListener(obj.conditions[i],show);
				}
			}
			
			
			
			obj.target = target;
			obj.modifier = modifyTips;
			obj.context = context;
			obj.conditions = conditions;
			obj.tips = null;
			obj.container = tipsContainer;
			obj.x = x;
			obj.y = y;
//			target.addEventListener(Event.REMOVED_FROM_STAGE,destroyTooltips);
			
			for(i=0,l =conditions.length;i<l;i++){
				target.addEventListener(conditions[i],show);
			}
		}
		
		private static function destroyTooltips(e:Event):void{
			e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE,destroyTooltips);
			removeTooltips(e.currentTarget as DisplayObject);
		}
		
		public static function update(target:DisplayObject,context:Object):void{
			var obj:Object = findTipsConfig(target);
			if(obj!=null){
				obj.context = context;
				(obj.target as DisplayObject).dispatchEvent(new Event(TOOLTIPS_UPDATE));
			}
		}
		
		
		public static function clear():void{
			var i:int,l:int;
			for(i=0,l=tipsObject.length;i<l;i++){
				if(tipsObject[i].tips!=null&&tipsObject[i].container.contains(tipsObject[i].tips)){
					tipsObject[i].tips.visible = false;
				}
			}
		}
		
		public static function openTooltips(...rest):*{
			enableToolTips = true;
		}
		
		private static var _enableToolTips:Boolean = true;
		public static function set enableToolTips(value:Boolean):void{
			_enableToolTips = value;
			if(!enableToolTips){
				clear();
			}
		}
		
		public static function get enableToolTips():Boolean{
			return _enableToolTips;
		}
		
		private static function show(e:Event):void{
			if(!enableToolTips){
				return;
			}
			var obj:Object = findTipsConfig(e.currentTarget as DisplayObject);
			if(obj!=null){
				obj.tips = obj.modifier(e,obj.target,obj.context,obj.tips,obj.container,obj.x,obj.y);
				if(obj.tips!=showingTips&&obj.tips.visible){
					if(showingTips!=null){
						showingTips.visible = false;
					}
					showingTips = obj.tips as DisplayObject;			
				}
				if(obj.tips!=null){
					var dobj:DisplayObject = obj.container.addChild(obj.tips as DisplayObject);
					if(dobj.x == 0)
					{
						dobj.x = (e as MouseEvent).stageX - obj.container.x;
					}else
					{	
						dobj.x = dobj.x;
					}
					if(dobj.y == 0)
					{
						dobj.y = (e as MouseEvent).stageY - obj.container.y;
					}else
					{	
						dobj.y = dobj.y;
					}
//					if((dobj.y+dobj.height) > Config.STATE_HEIGHT)
//					{
//						dobj.y = Config.STATE_HEIGHT - dobj.height;
//						dobj.x += 2; 	
//					}
//					if((dobj.x + dobj.width) > Config.STAGE_WIDTH)
//					{
//						dobj.x = Config.STAGE_WIDTH - dobj.width;
//						dobj.y += 2;
//					}
				}
			}
		}
		
		public static function removeTooltips(target:DisplayObject):void{
			var obj:Object = findTipsConfig(target);
			if(obj!=null){
				ArrayTools.del(tipsObject,obj);
				for(var i:int=0,l:int=obj.conditions.length;i<l;i++){
					target.removeEventListener(obj.conditions[i].toString(),show);
				}				
				delete(obj.target);
				delete(obj.modifier);
				delete(obj.context);
				delete(obj.conditions);
				if(obj.tips!=null&&obj.container.contains(obj.tips)){
					obj.container.removeChild(obj.tips);
				}
				delete(obj.tips);
				delete(obj.container);// = tipsContainer;
			}
		}
		
		
		public static function defaultModifier(e:Event,target:DisplayObject,context:Object,tips:DisplayObject,container:DisplayObjectContainer,xs:Number,ys:Number):DisplayObject{
			var x:Number,y:Number;
			if(e is MouseEvent){
				x = MouseEvent(e).stageX;
				y = MouseEvent(e).stageY;
			}
			if(tips ==null){
				tips = new defaultTooltips(context);
				tips.visible = false;
			}
			if(e.type == TOOLTIPS_UPDATE){
				if(tips is defaultTooltips){
					(tips as defaultTooltips).update(context);
				}
			}
			if(e is MouseEvent){
				var mx:Number = (e as MouseEvent).stageX;
				var my:Number = (e as MouseEvent).stageY;
				var oldv:Boolean = tips.visible;
				if(target.hitTestPoint(x,y,true)&&e.type!=MouseEvent.MOUSE_OUT){
					tips.visible = true;	
				}else{
					tips.visible = false;
				}
				if(!oldv&&tips.visible){
					var rect:Rectangle = target.getBounds(container);
					tips.x = rect.right;
					tips.y = rect.top+int(rect.height*0.618);
					
					//adjust to see all
					var tRect:Rectangle = new Rectangle(tips.x,tips.y,tips.width,tips.height);
					var oRect:Rectangle = target.getBounds(target.stage);
					var sRect:Rectangle = new Rectangle(0,0,target.stage.stageWidth,target.stage.stageHeight); 
					var iRect:Rectangle = tRect.intersection(sRect);
					if(iRect.width<tRect.width){
						if((sRect.right-oRect.right)<tRect.width){
							//move down
							tRect.offset(sRect.right-tRect.right,oRect.bottom-tRect.top);
							//return;
							if(false&&(sRect.bottom-oRect.bottom)<tRect.height){
								//move to left margin
								tRect.x = oRect.x-tRect.width;
								tRect.offset(0,sRect.bottom-tRect.bottom);
								if((oRect.left-sRect.left)<tRect.width){
									//move to up margin
									tRect.x = sRect.x;
									tRect.y = oRect.y-tRect.height;
									if((oRect.top-sRect.top)<tRect.height){
										//return to origin;
										tRect.x = tips.x;
										tRect.y = tips.y;
									}
								}
							}
						}
					}
					iRect = tRect.intersection(sRect);
					if(iRect.height<tRect.height){
						if(oRect.right<=tRect.left){
							tRect.y = sRect.bottom-tRect.height;
						}else{
							tRect.y = oRect.y-tRect.height;
						}
					}
					iRect = tRect.intersection(sRect);
					if(iRect.height<tRect.height||iRect.width<tRect.width){
						
					}else{
						tips.x = tRect.x;
						tips.y = tRect.y;
					}
					
				}
			}
			tips.x = xs;
			tips.y = ys;
			return tips;
		}
	}
}
import com.maqie.view.UM;
import com.yy.yyui.Scale9GridDisplayObject;
import com.yy.yyui.text.TextLabel;
import com.util.tools.GT;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.filters.ColorMatrixFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextFieldAutoSize;


class defaultTooltips extends Sprite{
	private var bg:DisplayObject;
	private var tl:TextLabel;
	private var dobj:DisplayObject;
	public function defaultTooltips(info:Object){
		super();
		this.mouseEnabled = false;
		this.mouseChildren = false;
		tl = new TextLabel();
		tl.supportUBB = true;
		tl.autoSize = TextFieldAutoSize.LEFT;
		tl.selectable = true;		
//		bg = UM.scale9(UM.defualtTipsBackground);
		bg = GT.getRectGraphic(50,50,0xff0000, 0xffffcc, 1);
		var sRect:Rectangle = bg.scrollRect;
		GT.fulFill(bg,0x404040,0.8);
		var sp:Sprite = new Sprite();
		sp.addChild(bg);
		bg = new Scale9GridDisplayObject(bg,sRect);
		if(info is DisplayObject){
			dobj = info as DisplayObject;
			this.addChild(dobj);
			tl.visible = false;
			bg.visible = false;
		}else{
			tl.text = info.toString();
			tl.adjustWidth();
			this.addChild(UM.visualize(tl,[new Point(5,5),bg]));
		}
	}
	public function update(info:Object):void{
		if(info is DisplayObject){
			GT.replaceChild(this,info,dobj,true,true,false,false);
			dobj = info as DisplayObject;
			dobj.visible = true;
			tl.visible =false;
			bg.visible =false;
		}else{
			tl.visible = true;
			bg.visible = true;
			if(dobj!=null){
				dobj.visible = false;
			}
			var oldW:Number = tl.width;
			var oldH:Number = tl.height;
			tl.text = info.toString();
			tl.adjustWidth();
			var diffW:Number = tl.width - oldW;
			var diffH:Number =  tl.height - oldH;
			bg.width += diffW;
			bg.height += diffH;
		}
	}
}